#ifndef mainpanel_h
#define mainpanel_h

#include "juce_amalgamated.h"
#include "CircleComponent.h"
#include "SwitchablePanel.h"
#include "BoundedPanel.h"
#include <iostream>

class MainPanel: public BoundedPanel, public SwitchablePanel, public Timer
{
private:
	const static int numCircles = 6;
	Image* image;
	CircleComponent** circles;
	GradientBrush* yellowGradient;
	String currentText;
	int animatingCircle;

protected:
	void timerCallback()
	{
		circles[animatingCircle]->incrementAngle();
	}
public:
	MainPanel(MasterComponent* master, int w, int h) :
		BoundedPanel(w, h), SwitchablePanel(master)
	{
		int x, y;
		int radius = 140;
		int centreX = width / 2 - radius / 2;
		int centreY = height / 2 - radius / 2;
		int bigRadius = (int)(height * 0.32);

		circles = new CircleComponent*[numCircles];

		String names[] = { T("Utilities"), T("Nutrition Guide"), T("Meal List"), T("Food List"), T(
				"Grocery List"), T("Meal Scheduler") };

		String filenames[] = { T("images/icons/utilities.png"), T("images/icons/nutrition.png"), T(
				"images/icons/meallist.png"), T("images/icons/foodlist.png"), T(
				"images/icons/grocerylist.png"), T("images/icons/schedule.png") };

		for(int i = 0; i < numCircles; i++) {
			x = centreX + (int)(bigRadius * cos(i * 2 * M_PI / float(numCircles)+0.14));
			y = centreY + (int)(bigRadius * sin(i * 2 * M_PI / float(numCircles)+0.14));

			addAndMakeVisible(circles[i] = new CircleComponent(this, i, x, y, radius, names[i], filenames[i]));
			circles[i]->setBounds(x, y, radius, radius);
		}

		yellowGradient = new GradientBrush(Colour(250, 250, 240), width / 2, height / 2,
				Colour(244, 230, 60), 0, 0, true);
	}

	~MainPanel()
	{
		for(int i = 0; i < numCircles; i++) {
			delete circles[i];
		}
		delete[] circles;
	}

	void paint(Graphics& g)
	{
		const static int textW = 190;
		const static int textH = 40;
		const static int fontsize = 24;
		Colour lightYellow(244, 250, 170);
		Rectangle textRect(width / 2 - textW / 2, height / 2 - textH / 2, textW, textH);
		Rectangle r(0, 0, width, height);

		g.setColour(Colours::grey);
		g.setBrush(yellowGradient);
		g.fillRoundedRectangle(r, 100);
		g.setColour(Colours::darkred);
		MiscGraphics::drawRoundedRect(g, r, 100, 2);

		g.setColour(Colours::white);
		g.fillRoundedRectangle(textRect, 6);
		g.setColour(Colours::darkgrey);
		g.drawRoundedRectangle(textRect, 6, 1);
		g.setColour(Colours::black);
		g.setFont(fontsize, Font::plain);
		g.drawFittedText(currentText, width / 2 - textW / 2, height / 2 - textH / 2, textW, textH,
				Justification::centred, 1, 0.4f);
	}

	void setTextbox(String text)
	{
		currentText = String(text);
		repaint();
	}

	void circleClicked(int id)
	{
		switchPanels(MasterComponent::MAIN, id);
	}

	void animateCircle(int id)
	{
		animatingCircle = id;
	}

	void switchPanels(int from, int to)
	{
		master->switchPanels(from, to);
	}
};

#endif
